package com.sitia.web.controller.system;

import cn.hutool.core.util.StrUtil;
import com.sitia.common.core.controller.BaseController;
import com.sitia.common.core.page.TableDataInfo;
import com.sitia.system.domain.SqlParams;
import com.sitia.system.service.ISysSqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 数据库SQL 专用控制器
 */
@RestController
@RequestMapping("/system/sql")
public class SysSqlController extends BaseController {
    @Autowired
    private ISysSqlService sqlService;

    @PostMapping( "/check/select" )
    public TableDataInfo getList(@RequestBody SqlParams sql ) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        if ( StrUtil.isBlank( sql.getSql() ) ) {
            tableDataInfo.setCode( 500 );
            tableDataInfo.setMsg( "请输入" );
            return tableDataInfo;
        }
        try {
            return getDataTable( sqlService.selectSql( sql ) );
        } catch ( Exception e ) {
            tableDataInfo.setCode( 500 );
            tableDataInfo.setMsg( e.getCause().getMessage() );
            return tableDataInfo;
        }

    }
}
